package com.cloudkinto.service.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cloudkinto.common.RabbitQueueConstant;
import com.cloudkinto.entity.PlatformStoreDo;
import com.cloudkinto.service.consumer.vo.orderSyncRecord.PlatformOrderSyncRecordReq;
import com.cloudkinto.service.consumer.vo.rakuten.*;
import com.cloudkinto.service.platform.sync.PlatformOrderSyncRecordService;
import com.cloudkinto.service.platform.PlatformOrderRakutenSyncService;
import com.cloudkinto.service.platform.vo.rakuten.RakutenSyncReq;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;
import java.util.Date;

@Slf4j
@Component
@RequiredArgsConstructor
public class PlatformOrderRakutenConsumer {

    private final PlatformOrderRakutenSyncService platformOrderRakutenSyncService;

    private final PlatformOrderSyncRecordService platformOrderSyncRecordService;
    private final RabbitTemplate rabbitTemplate;

    /**
     * 自动确认100订单
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Rakuten_AutoConfirm))
    public void autoConfirmOrder(String msg) {
        String s =null;
        try {
            s = platformOrderRakutenSyncService.autoConfirmOrder(Long.parseLong(msg));
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }


    /**
     * 同步订单表 历史
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Rakuten_History_Sync))
    public void syncOrderRakutenHistory(String msg) {
        RakutenSyncReq req = JSONObject.parseObject(msg, RakutenSyncReq.class);
        String s =null;
        try {
            s = platformOrderRakutenSyncService.updateRakutenOrdersHistory(req);
        }catch (Exception e) {
            e.printStackTrace();
        }
        if (StringUtils.isNotBlank(s)) {
            log.error(s);
        }
    }



    /**
     * 同步订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Rakuten_Sync))
    public void syncOrderRakuten(String msg) {
        RakutenSyncReq req = JSONObject.parseObject(msg, RakutenSyncReq.class);
        String s =null;
        try {
            s = platformOrderRakutenSyncService.updateRakutenOrders(req);
        }catch (Exception e) {
            s = e.getMessage();
        }
        if (StringUtils.isNotBlank(s)) {
            rabbitTemplate.convertAndSend(RabbitQueueConstant.PlatformOrder_SyncRecord_Update, JSON.toJSONString(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s, null)));
//            platformOrderSyncRecordService.updateSyncRecord(new PlatformOrderSyncRecordReq(req.getSyncId(), 3, new Date(), null, s));
            log.error("乐天同步错误返回:" + s);
        }
    }



    /**
     * 更新乐天订单表
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Rakuten_Update))
    public void syncOrderRakutenUpdate(String msg) {
        RakutenOrderUpdateConReq req = JSONObject.parseObject(msg, RakutenOrderUpdateConReq.class);
        try {
            platformOrderRakutenSyncService.consumerUpdate(req);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 标发
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformStore_Rakuten_MarkShip))
    public void markShip(String msg) {
        RakutenMarkShipConsumerReq req = JSONObject.parseObject(msg, RakutenMarkShipConsumerReq.class);
        try {
            platformOrderRakutenSyncService.markShip(req);
        }catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 像乐天索取 标发状态
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.PlatformOrder_Rakuten_MarkStatus), concurrency = "1", containerFactory = "singleConsumerFactory")
    public void getMarkStatus(String msg) {
        if (StringUtils.isBlank(msg)) {
            return;
        }
        try {
            platformOrderRakutenSyncService.getMarkStatus(Long.parseLong(msg));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 库存回传
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.StockToPlatform_Rakuten), concurrency = "1", containerFactory = "singleConsumerFactory")
    public void setStock(String msg) {
        try {
            RakutenStockToPlatReq req = JSONObject.parseObject(msg, RakutenStockToPlatReq.class);
            platformOrderRakutenSyncService.setStock(req.getStockList(), req.getStoreDo(), req.getUserId());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同步平台商品
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Rakuten))
    public void getItem(String msg) {
        ItemUpdateConsumerReq req = JSONObject.parseObject(msg, ItemUpdateConsumerReq.class);
        try {
            String string = platformOrderRakutenSyncService.itemSearch(req.getStoreDo(), req.getParentSku(), req.getPlatformSku());
            if (StringUtils.isNotBlank(string)) {
                log.error("乐天同步平台商品失败：" + string);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 同步平台库存
     * @param msg
     */
    @RabbitListener(queuesToDeclare = @Queue(RabbitQueueConstant.GetPlatformProduct_Stock_Rakuten))
    public void inventoryGet(String msg) {
        PlatformStoreDo req = JSONObject.parseObject(msg, PlatformStoreDo.class);
        try {
            String string = platformOrderRakutenSyncService.inventoryBulkGetRange(req);
            if (StringUtils.isNotBlank(string)) {
                log.error("乐天同步平台库存失败：" + string);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }




}
